<template>
  <Navbar :title="title" left-text="返回" left-arrow @click-left="goBack" fixed placeholder></Navbar>

  <van-tabs v-model:active="active" animated sticky :offset-top="46" @click-tab="initRecord">
    <van-tab v-for="(item, index) in records" :key="index" :title="item.name">
      <div v-if="show">

        <div class="card" v-for="(item, index) in recordList">
          <van-cell title="Gundam商城" class="title">
            <template #icon>
              <img src="/assets/images/shop-avatar.png" alt="" width="30">
            </template>
          </van-cell>

          <div class="record_content">
            <!-- <h1>￥ 16.00</h1> -->
            <div v-if="item.cash > 0">
              <p>付款金额</p>
              <h1>
                <van-icon name="balance-o" color="#f1bf25" />
                {{ item.cash }}
              </h1>
            </div>
            <div v-else>
              <p>付款积分</p>
              <h1>
                <van-icon name="gem-o" color="#06c05f" />
                {{ item.point }}
              </h1>
            </div>
          </div>
          <van-cell-group :border="false">
            <van-cell>
              <template #title>
                <span class="custom-r" style="color: ##b1889d;">支付方式</span>
                <span class="">积分</span>
              </template>
            </van-cell>

            <van-cell>
              <template #title>
                <span class="custom-r" style="color: ##b1889d;">交易类型</span>
                <span class="">{{ item.state_text }}</span>
              </template>
            </van-cell>

            <van-cell title="消费时间" :value="item.createtime" />
          </van-cell-group>

          <van-cell-group style="border-top: 1px solid #e7e7e7;">
            <van-cell title="查看账单详情" is-link :to="{ path: '/user/base/record_info', query: { recordId: item.id } }" />
          </van-cell-group>
        </div>

      </div>
      <van-empty v-else image="https://fastly.jsdelivr.net/npm/@vant/assets/custom-empty-image.png" image-size="80"
        description="记录为空" />
    </van-tab>
  </van-tabs>

</template>

<script>
import Footer from "../Footer.vue";
import Navbar from "../Navbar.vue";
// import RecordList from "../RecordList.vue";
export default {
  name: "Record",
  data() {
    return {
      title: "消费记录",
      userid: "",
      active: 'active',
      show: true,
      recordList: [],
      records: [
        {
          name: '所有',
          state: 0
        },
        {
          name: '充值',
          state: 3
        },
        {
          name: '商品订单',
          state: 5
        },
        {
          name: '预约订单',
          state: 6
        },
        {
          name: '所有类型退款',
          state: -1
        },
        {
          name: '预约订单撤销',
          state: -2
        },
      ]
    };
  },
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    async initRecord(val) {
      this.show = true
      this.recordList = null
      // console.log(val);
      // console.log(this.records[val.name].state);
      // TODO 初始化消费记录表
      const data = {
        userid: this.userid,
        state: this.records[val.name].state
      }
      let res = await this.$api.record(data);

      if (res.code === 1) {
        this.$toast.loading({
          duration: 800,
          loadingType: 'spinner',
          onClose: () => {
            if (res.data.length > 0) {
              this.show = true
              this.recordList = res.data;
              // console.log(this.recordList);
            } else {
              this.show = false
              this.recordList = null
            }
          }
        })
      } else {
        this.$notify({
          type: "warning",
          message: res.msg,
          onClose: () => {
            this.show = false
            this.recordList = null
            return false;
          },
        });
      }
    },
  },
  components: {
    Navbar,
    Footer,
    // RecordList,
  },
  created() {
    // console.log(this.$cookies.get("ShopUser").id);
    this.userid = this.$cookies.get("ShopUser").id
      ? this.$cookies.get("ShopUser").id
      : "";

    const val = {
      name: 0
    }
    this.initRecord(val);
  },
};
</script>

<style scoped>
.title :deep(.van-cell__title) {
  display: flex;
  align-items: center;
  margin-left: 5px;
}

.card {
  width: 95%;
  border-radius: 10px;
  margin: 10px auto;
  box-shadow: 0 0 15px #ccc;
}

.card .title {
  border-bottom: 1px solid #e7e7e7;
}

.card .title :deep(.van-icon) {
  color: #2ba245;
}

.record_content {
  background: #fff;
  text-align: center;
  padding: 20px 0;
}

.record_content p {
  font-size: .875rem;
  margin-bottom: 10px;
}

.record_content h1 {
  font-size: 28px;
  /* color: #000; */
}

/* .record_content h1 :deep(.van-icon){
  color: #06c05f;
} */

.custom-r {
  margin-right: .75rem;
  color: #969799;
}

.goods-card {
  margin: 0;
  background-color: white;
}

.delete-button {
  height: 100%;
}
</style>
